#!/bin/bash

# 众筹项目启动脚本

echo "=== 启动众筹项目 ==="

# 启动后端
echo "启动后端服务..."
cd backend
npm install
npm start &
BACKEND_PID=$!

# 等待后端启动
sleep 3

# 启动前端
echo "启动前端服务..."
cd ../front
npm install
npm run serve &
FRONTEND_PID=$!

# 启动区块链节点
echo "启动本地Hardhat节点..."
cd ../my-hardhat-project
npm install
npx hardhat node &
NODE_PID=$!

# 等待节点启动
sleep 5

# 部署合约
echo "部署智能合约..."
npx hardhat run scripts/deploy.js --network localhost

echo "=== 所有服务已启动 ==="
echo "后端服务: http://localhost:4000"
echo "前端服务: http://localhost:8080"
echo "本地区块链节点: http://localhost:8545"
echo ""
echo "按 Ctrl+C 停止所有服务"

# 捕获 Ctrl+C 信号
trap "echo '正在停止服务...'; kill $BACKEND_PID $FRONTEND_PID $NODE_PID; exit" INT

# 保持脚本运行
wait
